package com.gmail.deathswing.colormix;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.util.FPSLogger;
import org.andengine.opengl.texture.Texture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class GameActivity extends SimpleBaseGameActivity {
	private static final int CAMERA_WIDTH = 720;
	private static final int CAMERA_HEIGHT = 480;
	
	private Camera mCamera;
	
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private ITextureRegion mNotesTextureRegion;
	
	
//	private static class MyRectangle extends Rectangle {
//
//		public MyRectangle(float pX, float pY, float pWidth, float pHeight) {
//			super(pX, pY, pWidth, pHeight);
//		}
//		
//		@Override
//		protected void onManagedUpdate(float pSecondsElapsed) {
//			if(this.mX < 0) {
//				this.mVelocityX = BALL_VELOCITY;
//			} else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
//				this.mVelocityX = -BALL_VELOCITY;				
//			}
//			
//			if(this.mY < 0) {
//				this.mVelocityY = BALL_VELOCITY;
//			} else if(this.mY + this.getWidth() > CAMERA_HEIGHT) {
//				this.mVelocityY = -BALL_VELOCITY;				
//			}
//			
//			super.onManagedUpdate(pSecondsElapsed);
//		}
//	}
//	
//	
//	private static class Ball extends AnimatedSprite {
//		public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion) {
//			super(pX, pY, pTextureRegion);
//			this.animate(100);
//		}
//
//		@Override
//		protected void onManagedUpdate(final float pSecondsElapsed) {
//			if(this.mX < 0) {
//				this.mVelocityX = BALL_VELOCITY;
//			} else if(this.mX + this.getWidth() > CAMERA_WIDTH) {
//				this.mVelocityX = -BALL_VELOCITY;				
//			}
//			
//			if(this.mY < 0) {
//				this.mVelocityY = BALL_VELOCITY;
//			} else if(this.mY + this.getWidth() > CAMERA_HEIGHT) {
//				this.mVelocityY = -BALL_VELOCITY;				
//			}
//			
//			super.onManagedUpdate(pSecondsElapsed);
//		}
//	}


	@Override
	public EngineOptions onCreateEngineOptions() {
		this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
		final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera);
		engineOptions.getAudioOptions().setNeedsMusic(true);
		
		return engineOptions;
	}

	@Override
	protected void onCreateResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 128, 128, TextureOptions.BILINEAR);
		this.mNotesTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "notes.png", 0, 0);
		this.mBitmapTextureAtlas.load();
		
	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());

		final Scene scene = new Scene();
		scene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));

		final float centerX = (CAMERA_WIDTH - this.mNotesTextureRegion.getWidth()) / 2;
		final float centerY = (CAMERA_HEIGHT - this.mNotesTextureRegion.getHeight()) / 2;

		final Sprite notes = new Sprite(centerX, centerY, this.mNotesTextureRegion, this.getVertexBufferObjectManager());
		scene.attachChild(notes);
		scene.registerTouchArea(notes);
		
//		scene.setOnAreaTouchListener(new IOnAreaTouchListener() {
//			
//			@Override
//			public boolean onAreaTouched(TouchEvent arg0, ITouchArea arg1, float arg2,
//					float arg3) {
//				
//				return true;
//			}
//		};

		return scene;
	}
}
